﻿Public Class FormPurchase

    Private Sub FormPurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.dgv)
        LoadData()

    End Sub

    Public Sub LoadData()
        'DateTimePicker1.Format = DateTimePickerFormat.Custom
        'DateTimePicker1.CustomFormat = "yyyy-mm-dd"
        'DateTimePicker2.Format = DateTimePickerFormat.Custom
        'DateTimePicker2.CustomFormat = "yyyy-mm-dd"

        'Me.dgv.DataSource = Cmd.ExecuteDataTable("select * from Purchase where PurchaseDate between'" & Me.DateTimePicker1.Text & "'and '" & Me.DateTimePicker2.Text & "' ")
        Cmd.Parameters("@d1") = Me.DateTimePicker1.Value
        Cmd.Parameters("@d2") = Me.DateTimePicker2.Value
        Cmd.Parameters("@code") = Me.TextBox1.Text
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select * from Purchase where PurchaseDate between @d1 and @d2 and PurchaseNo like '%'+@code+'%'")

    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        LoadData()
    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim frm As New FormPurchaseEditor()
        frm.ShowDialog()



    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

  
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        'If Me.dgv.SelectedRows.Count = 0 Then
        '    Exit Sub
        'End If

        'If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
        '    Exit Sub
        'End If

        'Sql.Begin()
        'Try
        '    Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
        '    Dim id = row.Cells("PurchaseId").Value
        '    Dim storeId = Sql.ExecuteScalar("SELECT StoreId FROM Purchase WHERE PurchaseId=" & id)
        '    Sql.ExecuteNonQuery("UPDATE Purchase SET Active=0 WHERE PurchaseId=" & id)
        '    Dim dtPurchaseDetail As DataTable = Sql.ExecuteDataTable("SELECT * FROM PurchaseDetail WHERE Active=1 AND  PurchaseId = " & id)
        '    For Each r As DataRow In dtPurchaseDetail.Rows
        '        Sql.ExecuteNonQuery("UPDATE Stock SET Qty = Qty - " & r("Qty") & " WHERE ProductId=" & r("ProductId") & " AND StoreId = " & storeId)
        '        Sql.ExecuteNonQuery("UPDATE PurchaseDetail SET Active=0 WHERE PurchaseDetailId=" & r("PurchaseDetailId"))
        '    Next

        '    Sql.Commit()
        '    LoadData()

        'Catch ex As Exception
        '    Sql.Rollback()
        '    MsgBox(ex.Message)

        'End Try 
        
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        LoadData()
    End Sub

   
End Class